package com.coscon.service.patternCommon.impl.mouse;

import com.coscon.model.IrcaCmd;
import com.coscon.service.patternCommon.ICommand;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.websocket.Session;
import java.awt.*;

/**
 * 鼠标指针移动
 */
@Slf4j
public class MousePointCmdImpl implements ICommand {
    private boolean debug = false;
    private Robot rb;
    //当前显示器分辨率
    private Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    public MousePointCmdImpl(Robot rb){
        this.rb = rb;
    }
    

    public Boolean execute(IrcaCmd cmd,  Session session) {
        //获取当前鼠标指针坐标
        Point location = MouseInfo.getPointerInfo().getLocation();
        int x = location.x;
        int y = location.y;
        int stp = 3;
        if(StringUtils.equalsIgnoreCase(cmd.getValue(),"UP" )){//鼠标滚轴向上
            y = y-stp;
        }else if(StringUtils.equalsIgnoreCase(cmd.getValue(),"DOWN" )){//鼠标滚轴向下
            y = y+stp;
        }else if(StringUtils.equalsIgnoreCase(cmd.getValue(),"LEFT" )){//鼠标滚轴向下
            x = x-stp;
        }else if(StringUtils.equalsIgnoreCase(cmd.getValue(),"RIGHT" )){//鼠标滚轴向下
            x = x+stp;
        }
        if(debug){
            log.debug("新的坐标x=" + x + ",y=" + y);
        }
        if(x<0){
            x = 0;
        }
        if(x>this.dimension.width){
            x = this.dimension.width;
        }
        if(y<0){
            y=0;
        }
        if(y>this.dimension.height){
            y = this.dimension.height;
        }
        //移动到新的坐标
        rb.mouseMove(x, y);
        return true;
    }

    @Override
    public Boolean executeByMac(IrcaCmd cmd, Session session) {
        return this.execute(cmd,session);
    }

    @Override
    public Boolean executeByWin(IrcaCmd cmd, Session session) {
        return this.execute(cmd,session);
    }

    @Override
    public Boolean executeByLinux(IrcaCmd cmd, Session session) {
        return this.execute(cmd,session);
    }

}
